package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.mapper.SysLoginLogMapper;
import com.li.service.SysLoginLogService;
import com.li.system.SysLoginLog;
import com.li.vo.SysLoginLogQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper,SysLoginLog> implements SysLoginLogService {

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    @Override
    public void loginLog(String username, Integer status, String ipaddr, String msg) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setStatus(status);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(msg);
        sysLoginLogMapper.insert(sysLoginLog);
    }

    //获取分页列表
    @Override
    public IPage<SysLoginLog> selectPage(Long page, Long limit, SysLoginLogQueryVo sysLoginLogQueryVo) {

        //创建page对象
        Page<SysLoginLog> logPage = new Page<>(page, limit);
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysLoginLogQueryVo.getUsername())){
            wrapper.like(SysLoginLog::getUsername,sysLoginLogQueryVo.getUsername());
        }
        if (!StringUtils.isEmpty(sysLoginLogQueryVo.getCreateTimeBegin())){
            wrapper.ge(SysLoginLog::getCreateTime,sysLoginLogQueryVo.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(sysLoginLogQueryVo.getCreateTimeEnd())){
            wrapper.le(SysLoginLog::getCreateTime,sysLoginLogQueryVo.getCreateTimeEnd());
        }
        //调用mapper方法
        IPage<SysLoginLog> selectPage = sysLoginLogMapper.selectPage(logPage, wrapper);

        return selectPage;
    }


}
